package com.mf.enums;

import lombok.Getter;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
@Getter
public enum EnumViewContentType {
    DEFAULT("default", "application/octet-stream"),
    PNG("png", "image/png"),
    JPEG("jpeg", "image/jpeg"),
    JPG("jpg", "image/jpeg"),
    GIF("gif", "image/gif"),
    WBMP("wbmp", "image/vnd.wap.wbmp"),
    TIFF("tiff", "image/tiff"),
    JFIF("jfif", "image/jpeg"),
    TIF("tif", "image/tiff"),
    FAX("fax", "image/fax"),
    JPE("jpe", "image/jpeg"),
    NET("net", "image/pnetvue"),
    RP("rp", "image/vnd.rn-realpix"),
    DOCX("docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"),
    DOC("doc", "application/msword"),
    XLS("xls", "application/vnd.ms-excel"),
    XLSX("xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"),
    PPT("ppt", "application/vnd.ms-powerpoint"),
    PPTX("pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation"),
    PDF("pdf", "application/pdf"),
    TXT("txt", "text/plain"),
    HTML("html", "text/html"),
    CSS("css", "text/css"),
    JS("js", "application/javascript"),
    JSON("json", "application/json"),
    XML("xml", "application/xml"),
    ZIP("zip", "application/zip"),
    GZIP("gz", "application/gzip"),
    TAR("tar", "application/x-tar"),
    RAR("rar", "application/x-rar-compressed"),
    SEVENZIP("7z", "application/x-7z-compressed"),
    MP3("mp3", "audio/mpeg"),
    WAV("wav", "audio/wav"),
    OGG("ogg", "audio/ogg"),
    MP4("mp4", "video/mp4"),
    WEBM("webm", "video/webm"),
    AVI("avi", "video/x-msvideo"),
    MKV("mkv", "video/x-matroska"),
    ICO("ico", "image/x-icon");

    private final String prefix;
    private final String type;

    private static final Map<String, EnumViewContentType> ENUM_MAP = new HashMap<>();

    static {
        EnumViewContentType[] values = values();
        for (EnumViewContentType value : values) {
            ENUM_MAP.put(value.getPrefix(), value);
        }
    }

    /**
     * 根据文件名前缀获取文件类型
     */
    public static String getTypeByPrefix(String prefix) {
        EnumViewContentType enumViewContentType = ENUM_MAP.get(prefix);
        if (enumViewContentType == null) {
            return prefix;
        }
        return enumViewContentType.getType();
    }

    /**
     * 获取文件类型
     */
    public static String getContentType(String fileName) {
        if (fileName == null || fileName.isEmpty()) {
            return DEFAULT.getType();
        }
        String prefix = fileName.substring(fileName.lastIndexOf(".") + 1);
        String type = getTypeByPrefix(prefix);
        if (type != null && !type.isEmpty()) {
            return type;
        }
        return DEFAULT.getType();
    }

    /**
     * 判断是否是图片类型
     */
    public static boolean isImageType(String contentType) {
        return contentType.startsWith("image/");
    }

    /**
     * 编码文件名
     */
    public static String getEncodedFileName(String fileName) {
        try {
            return URLEncoder.encode(fileName, StandardCharsets.UTF_8)
                    .replaceAll("\\+", "%20");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    EnumViewContentType(String prefix, String type) {
        this.prefix = prefix;
        this.type = type;
    }


}